package com.example.util;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.example.inter.LoginCallBack;
import com.example.inter.RegisterInfo;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * Created by Administrator on 2016/3/3.
 */
public class RegisterStudent implements RegisterInfo {
    @Override
    public void sendSignInfo(String student_name, String student_number, String email, String studentPassword,final LoginCallBack loginCallBack) {

        RequestParams params = new RequestParams();
        params.addBodyParameter("StudentNumber",student_number);
        params.addBodyParameter("StudentName",student_name);
        params.addBodyParameter("StudentEmail",email);
        params.addBodyParameter("StudentPassword",studentPassword);
        params.addBodyParameter("StudentPic","/pic/head.png");
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.POST,
                "http://192.168.191.1/app/index.php/student/reg", params,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {

                        loginCallBack.onSuccess(responseInfo.result);

                    }

                    @Override
                    public void onStart() {


                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        //// TODO: 2015/12/18  ∂
                        loginCallBack.onFailed();
                    }


                });


    }

    @Override
    public boolean judeValid(Context context, EditText studentName, EditText studentNumber, EditText studentPassword, EditText studentEmail) {

        studentName.setError(null);
        studentPassword.setError(null);
        studentEmail.setError(null);
        studentNumber.setError(null);

        String name = studentName.getText().toString().trim();
        String pass = studentPassword.getText().toString().trim();
        String em = studentEmail.getText().toString().trim();
        String tId = studentNumber.getText().toString().trim();

        boolean cancel = false;
        View focusView = null;


        if (TextUtils.isEmpty(pass)) {
            studentPassword.setError("密码不能为空");
            focusView = studentPassword;
            cancel = true;
        }
        if (TextUtils.isEmpty(em)) {
            studentEmail.setError("邮箱不能为空");
            focusView = studentEmail;
            cancel = true;
        }
        if (TextUtils.isEmpty(tId)) {
            studentNumber.setError("学号不能为空");
            focusView = studentNumber;
            cancel = true;
        }
        if(!em.contains("@")){
            studentEmail.setError("此邮箱无效");
            focusView = studentEmail;
            cancel = true;
        }
        if (TextUtils.isEmpty(name)) {
            studentName.setError("用户名不能为空");
            focusView = studentName;
            cancel = true;
        }

        // Check for a valid studentPassword, if the user entered one.
        if (!TextUtils.isEmpty(pass) && !isPasswordValid(pass)) {
            studentPassword.setError("密码太短");
            focusView = studentPassword;
            cancel = true;
        }


        if (cancel) {
            focusView.requestFocus();
        } else {
        }
        return cancel;
    }

    private boolean isPasswordValid(String password) {
        return password.length() > 4;
    }
    

}
